﻿using DotNet.Utilities.ConsoleHelper;
using PropertyChanged;
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Input;
using DotNet.Utilities;
using WPFTemplateLib.WpfHelpers;

namespace WPFPractice.Windows
{
    public partial class WinImage : Window
    {
        public WinImage()
        {
            InitializeComponent();
            DataContext = new WinImageViewModel();
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class WinImageViewModel : BindableBase
    {
        public WinImageViewModel()
        {
            Console.SetOut(new ConsoleWriter(s =>
            {
                ShowInfoUc(s);
            }));

            SetCommandMethod();
        }

        #region 绑定

        /// <summary>
        /// 图片资源的路径字符串
        /// </summary>
        public string ImagePathStr { get; set; }

        /// <summary>
        /// 图片资源的路径Uri
        /// </summary>
        public Uri ImagePathUri { get; set; }

        /// <summary>
        /// 图片Stream数据
        /// </summary>
        public Stream ImageStream { get; set; }

        /// <summary>
        /// 图片字节数组数据
        /// </summary>
        public byte[] ImageBytes { get; set; }

        #endregion

        /// <summary>
        /// 数据初始化
        /// </summary>
        private void InitValue()
        {
            try
            {
                ImagePathStr = "pack://application:,,,/WPFPractice;component/Resources/Images/hu.jpg";
                Console.WriteLine($"ImagePathStr：{ImagePathStr}");

                ImagePathUri = new Uri(ImagePathStr, UriKind.RelativeOrAbsolute);
                Console.WriteLine($"ImagePathUri：{ImagePathUri}");

                //获取的是 System.IO.UnmanagedMemoryStream;
                var resourceStream = Application.GetResourceStream(ImagePathUri)?.Stream;
                if (resourceStream == null)
                {
                    Console.WriteLine($"ResourceStream 获取失败");
                    return;
                }

                //System.Windows.Media.ImageSourceConverter 不支持 UnmanagedMemoryStream；
                //ImageStream = resourceStream;

                Image image = Image.FromStream(resourceStream);
                ImageBytes = ImageClass.ImageToBytes(image);
                Console.WriteLine($"ImageBytes 大小：{ImageBytes.Length}");

                //ImageSourceConverter.CanConvertFrom() 不支持 MemoryStream，实际上 ConvertFrom() 是支持的，在转换器 AllTypeToImageSourceConverter 中已处理。
                Stream iamgeStream = new MemoryStream(ImageBytes);
                ImageStream = iamgeStream;
                Console.WriteLine($"ImageStream 大小：{ImageStream.Length}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"数据初始化出错：{ex}");
            }
        }

        #region 命令

        /// <summary>
        /// [命令] 载入事件转命令
        /// </summary>
        public ICommand LoadedCmd { get; set; }

        #endregion

        /// <summary>
        /// 命令方法赋值(在构造函数中调用)
        /// </summary>
        private void SetCommandMethod()
        {
            LoadedCmd = new RelayCommand(obj => true, obj =>
            {
                InitValue();
            });
        }
    }
}
